<?php
/**
 * @name Bootstrap
 * @author box-e470\qiph
 * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php
 * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
use Yaf\Loader;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Disptcher;
use Illuminate\Container\Container;

class Bootstrap extends \Yaf\Bootstrap_Abstract{

    public function _initConfig() {
		//把配置保存起来
		$arrConfig = \Yaf\Application::app()->getConfig();
		\Yaf\Registry::set('config', $arrConfig);
	}

// 将composer引入进来
    public function _initLoader(){
        $loader = Loader::getInstance();
        $autoload = APPLICATION_PATH . '/vendor/autoload.php';
        if (file_exists($autoload)){
            $loader->import($autoload);
        }
    }
//    数据库初始化
    public function _initDbAdapter(){
        $capsule=new Capsule();
//        数据库连接数据
        $capsule->addConnection(
            [
                //yaf利用eloquent对数据库进行集成
                'driver'=>'mysql',
                'host'=>'localhost',
                'database'=>'weblog',
                'username'=>'root',
                'password'=>'root',
                'charset'=>'utf8',
                'collation'=>'utf8_unicode_ci',
                'prefix'=>''
            ]
        );
        $capsule->setEventDispatcher(new \Illuminate\Events\Dispatcher(new \Illuminate\Container\Container));
//        设为全局可用
        $capsule->setAsGlobal();
//        启动eloquent
        $capsule->bootEloquent();
//        定义别名，通过这个别名可以直接对表进行操作
        class_alias(Illuminate\Database\Capsule\Manager::class,'DB');
    }

	public function _initPlugin(\Yaf\Dispatcher $dispatcher) {
		//注册一个插件
		$objSamplePlugin = new SamplePlugin();
		$dispatcher->registerPlugin($objSamplePlugin);
	}

	public function _initRoute(\Yaf\Dispatcher $dispatcher) {
		//在这里注册自己的路由协议,默认使用简单路由
        $route = $dispatcher->getRouter();
        $routeArr = new \Yaf\Route\Simple("m","c","a");
        $route->addRoute("name",$routeArr);
	}
	
	public function _initView(\Yaf\Dispatcher $dispatcher){
		//在这里注册自己的view控制器，例如smarty,firekylin,注册完后会自动寻找这个控制器
//        相当于setScriptPath()方法
//        $dispatcher->initView(APPLICATION_PATH . '/application/templates');

	}
}
